package com.xjggb.stream;


import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/*
*结果集收集
* */
public class Demo02 {
    public static void main(String[] args) {
        show7();

    }




    /*
    * 并行流
    * */

    public static void show7(){

        List<Integer> collect = Stream.of(1, 2, 3, 7, 5, 6, 9, 5, 5, 8, 69, 2).parallel()
                .filter(p -> {
                    System.out.println(Thread.currentThread() + " s=" +p);
                    return p > 2;
                }).collect(Collectors.toList());
        collect.forEach(o->{
            System.out.println("o = " + o);
        });
    }


    /*
    * 分区操作
    * */
    public static void show6(){
        Map<Boolean, List<Person>> collect = getListPerson().stream()
                .collect(Collectors.partitioningBy(p -> p.getAge() > 18));
        collect.forEach((k,v)-> System.out.println( k+"\t " + v.get(0)));

    }



    /*
    * 多级分组
    * */
    public static void show5(){

        Map<String, Map<String, List<Person>>> collect = getListPerson()
                .stream()
                .collect(Collectors.groupingBy(Person::getName,
                        Collectors.groupingBy(p -> p.getAge() >= 18 ? "成年" : "未成年")));
        collect.forEach((k,v)->{
            System.out.println("k = " + k);
            v.forEach((k1,v1)->{
                System.out.println("\t"+k1 + "=" + v1.get(0));
            });
        });
    }

    /*
    * 分组计算
    * */
    public static void show4(){

        Map<String, List<Person>> collect = getListPerson()
                .stream()
                .collect(Collectors.groupingBy(Person::getName));

        collect.forEach((k,v)-> System.out.println("k = " + k+"="+v));


        // 根据年龄分组 如果大于等于18 成年否则未成年
        Map<String, List<Person>> collect1 = getListPerson().stream()
                .collect(Collectors.groupingBy(p -> p.getAge() >= 18 ? "成年" : "未成年"));

        collect1.forEach((k,v)-> System.out.println("k = " + k+"="+v));


    }


    /*
    * 对流做数据计算
    * */
    public static void show3(){
    //获取年龄最大值
        Optional<Person> collect = getListPerson()
                .stream()
                .collect(Collectors.maxBy((o1, o2) -> o1.getAge() - o2.getAge()));
        System.out.println("最大年龄" + collect.get());

    //最小年龄
        Optional<Person> collect1 = getListPerson()
                .stream()
                .collect(Collectors.minBy((o1, o2) -> o1.getAge() - o2.getAge()));
        System.out.println("最小年龄" + collect1.get());


     //求和

        Integer collect2 = getListPerson()
                .stream()
                .collect(Collectors.summingInt(Person::getAge));
        System.out.println("年龄求和" + collect);

    // 平均值
        Double collect3 = getListPerson()
                .stream()
                .collect(Collectors.averagingInt(Person::getAge));
        System.out.println("年龄平均值= " + collect3);

     //统计数量
        Long collect4 = getListPerson()
                .stream()
                .collect(Collectors.counting());
        System.out.println("数量统计" + collect4);


    }





    /*
    * 收集到数组中
    * */
    public static void show02(){
        String[] strings = getListString()
                .stream()
                .toArray(String[]::new);
        System.out.println("Arrays.toString(strings) = " + Arrays.toString(strings));

    }

    /*
    * 收集到集合中
    * */
    public static void show01(){
        List<String> collect = getListString()
                .stream()
                .collect(Collectors.toList());
        collect.forEach(System.out::println);
        System.out.println("================= ");

        Set<String> collect1 = getListString()
                .stream()
                .collect(Collectors.toSet());
        collect1.forEach(System.out::println);

        /*
        * 设置收集的类型
        * */
        ArrayList<String> collect2 = getListString()
                .stream()
                .collect(Collectors.toCollection(ArrayList::new));

        HashSet<String> collect3 = getListString()
                .stream()
                .collect(Collectors.toCollection(HashSet::new));



    }


    public static ArrayList<String> getListString(){
        ArrayList<String> list = new ArrayList<>();
        list.add("aa");
        list.add("bb");
        list.add("cc");
        list.add("aa");
        list.add("aa");
        list.add("aa");
        return list;
    }

    public static ArrayList<Person> getListPerson(){

        ArrayList<Person> people = new ArrayList<>();
        people.add(new Person("张三", 18));
        people.add( new Person("李四", 22));
        people.add(new Person("张三", 13));
        people.add(new Person("王五", 15));
        people.add(new Person("张三", 19));
        return people;
    }
    
    public static ArrayList<Person> groupingByList(){
        ArrayList<Person> people = new ArrayList<>();
        people.add( new Person("张三", 18, 175));
        people.add( new Person("李四", 22, 177));
        people.add( new Person("张三", 14, 165));
        people.add( new Person("李四", 15, 166));
        people.add( new Person("张三", 19, 182));
        return people;
    }
    

}
